package com.ershui.blog.article.service.impl;

import com.ershui.blog.article.domain.LDiary;
import com.ershui.blog.article.mapper.LDiaryMapper;
import com.ershui.blog.article.service.LDiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 日志操作管理
 */
@Service
public class LDiaryServiceImpl implements LDiaryService {
    @Autowired
    private LDiaryMapper diaryMapper;
    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public int insert(LDiary record) throws ParseException {
        Date createDate=sdf.parse(record.getCreateDateStr());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(createDate);
        int year = calendar.get(Calendar.YEAR);
        record.setYear(year);
        record.setCreateDate(createDate);
        return diaryMapper.insert(record);
    }

    @Override
    public int insertSelective(LDiary record) {
        return diaryMapper.insertSelective(record);
    }

    @Override
    public List<LDiary> selectDiaryList() {
//        List<Integer>  years=diaryMapper.selectYear();
        List<LDiary> list=diaryMapper.selectDiaryList();
        for(LDiary diary:list){
            diary.setCreateDateStr(sdf.format(diary.getCreateDate()));
        }

        return list;
    }

    @Override
    public void updateDiary(LDiary diary) throws ParseException {
        Date createDate=sdf.parse(diary.getCreateDateStr());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(createDate);
        int year = calendar.get(Calendar.YEAR);
        diary.setYear(year);
        diary.setCreateDate(createDate);
        diaryMapper.updateDiary(diary);
    }

    @Override
    public void deleteDiaryById(Long id) {
        diaryMapper.deleteDiaryById(id);
    }
}
